#include <iostream>
#include <thread>
#include <future>
#include <chrono>

// async接口的学习使用

int Add(int num1, int num2)
{
    std::cout << "加法！\n";
    return num1 + num2;
}

int main()
{
    std::cout << "-------1---------\n";
    std::future<int> fut = std::async(std::launch::deferred, Add, 11, 22); // 选择延迟策略时，异步线程会等到调用get时才执行函数
    // std::future<int> fut = std::async(std::launch::async, Add, 11, 22); // 选择异步策略时，异步线程会立即执行函数
    std::this_thread::sleep_for(std::chrono::seconds(1));
    std::cout << "-------2---------\n";
    std::cout << "获取结果！\n";
    int ret = fut.get();
    std::cout << "-------3---------\n";
    std::cout << ret << std::endl;
    return 0;
}